<%@ page contentType="text/html; charset=utf-8" %>

<%@ include file="/WEB-INF/jsp/includeTaglib.jsp" %>

<tr>
  <td>
    <table width="100%" border="0" cellpadding="0" cellspacing="5">
      <tr>
        <td>
          <table width="100%" border="0" cellpadding="0" cellspacing="0">
            <tr>
              <td class="dashTopLeft"><img src="images/spacer.gif" width="7" height="7"></td>
              <td class="dashTop" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
              <td class="dashTopRight"><img src="images/spacer.gif" width="7" height="7"></td>
            </tr>
            <tr>
              <td class="dashLeft"><img src="images/spacer.gif" width="7" height="7"></td>
              <td align="center">
                <table width="100%" border="0" cellpadding="0" cellspacing="3">
                  <form id="searchForm" action="<c:url value='searchFunctionTemplateByPage.html' />" method="get">
                  <tr>
                    <td width="10%" height="20" class="globalText">函数模板编号</td>
                    <td width="20%" height="20" class="globalText">
                    	<input type="text" class="txtbox" size="20" id="functionTemplateIdTxt" name="functionTemplateIdTxt" value="" />
                    </td>
                    <td width="10%" height="20" class="globalText">函数名称</td>
                    <td width="20%" height="20" class="globalText">
                    	<input type="text" class="txtbox" size="20" name="functionName" value="" />
                    </td>
                    <td width="10%" height="20" class="globalText">函数类型</td>
                    <td width="10%" height="20" class="globalText">
                    	<select name="functionTypeSlt">
                    	  <option value="">----</option>
                    	  <option value="0" selected>数据函数</option>
                    	  <option value="1">系统函数</option>
                    	</select>
                    </td>
                    <td width="20%" >&nbsp;</td>
                  </tr>
                  <tr id="sysFunctionTR">
                    <td width="114" height="20" class="globalText">函数内容</td>
                    <td height="20" class="globalText">
                    	<select name="sysFunctionSlt">
                    	  <option value="0">日期</option>
                    	</select>
                    </td>
                  </tr>
                  <tr id="relationTableTR">
                    <td width="114" height="20" class="globalText">对应表</td>
                    <td height="20" class="globalText">
                    	<select name="relationTableSlt">
                    	  <option value="">----</option>
                    	  <option value="dorder">运单表</option>
                    	  <option value="bdepartment">组织结构表</option>
                    	  <option value="dclient">客户表</option>
                    	</select>
                    </td>
                  </tr>
                  <tr id="relationFieldTR">
                    <td width="114" height="20" class="globalText">对应字段</td>
                    <td height="20" class="globalText">
                    	<select name="relationFieldSlt">
                    	  <option value="">----</option>
                    	  <option value="orderid">运单编号</option>
                    	  <option value="cargono">货号</option>
                    	  <option value="cargoname">货物名称</option>
                    	  <option value="cargocount">件数</option>
                    	  <option value="clientid">客户编号</option>
                    	  <option value="shipper">发货人</option>
                    	  <option value="shipperphone">发货人电话</option>
                    	  <option value="shipperMP">发货人手机</option>
                    	  <option value="signaturer">收货人</option>
                    	  <option value="signaturephone">收货人电话</option>
                    	  <option value="signatureMP">收货人手机</option>
                    	  <option value="totalfee">总运费</option>
                    	  <option value="sendfee">已付费</option>
                    	  <option value="arrivefee">提付费</option>
                    	  <option value="monthfee">月结费</option>
                    	  <option value="gatherfee">代收款</option>
                    	  <option value="loaddeptid">起运地公司编号</option>
                    	  <option value="unloadeptid">目的地公司编号</option>
                    	  <option value="sendcargotype3">送货方式(1-自提，2-送货)</option>
                    	</select>
                    </td>
                  </tr>
                  <tr id="associatedFieldTR">
                    <td width="114" height="20" class="globalText">关联字段</td>
                    <td height="20" class="globalText">
                    	<select name="associatedFieldSlt">
                    	  <option value="">----</option>
                    	</select>
                    </td>
                  </tr>
                  <tr>
                    <td align="left"><input type="button" id="searchBtn" class="btnStyle" value="  查询  "></td>
                  </tr>
                  </form>
                </table>
              </td>
              <td class="dashRight"><img src="images/spacer.gif" width="7" height="7"></td>
            </tr>
            <tr>
              <td class="dashBottomLeft"><img src="images/spacer.gif" width="7" height="7"></td>
              <td class="dashBottom" width=100%><img src="images/spacer.gif" width="7" height="7"></td>
              <td class="dashBottomRight"><img src="images/spacer.gif" width="7" height="7"></td>
            </tr>
          </table>
        </td>
      </tr>
    </table>
  </td>
</tr>

<script type="text/javascript">

	$(document).ready(function() {
    var searchBtn = $("#searchBtn");
    var functionTemplateIdTxt = $("#functionTemplateIdTxt");
		
    functionTemplateIdTxt.keypress(function(e) {
    	if (e.which == 13) {//回车按下
		    doSearch();
    		return false;
    	}
    });
	
    searchBtn.click(function() {
    	doSearch();						  
		});

		function doSearch() {
			var functionTemplateIdTxtVal =  functionTemplateIdTxt.val().trim();
    	functionTemplateIdTxt.val(functionTemplateIdTxtVal);
    	
    	document.getElementById("searchForm").submit();
		}

	    
			//数据函数
	    var functionTypeSlt = $("select[name=functionTypeSlt]");

	    var relationTableSlt = $("select[name=relationTableSlt]");
	    var relationFieldSlt = $("select[name=relationFieldSlt]");
	 	  var associatedFieldSlt = $("select[name=associatedFieldSlt]");

	 	  
	    var relationTableTR = $("#relationTableTR");
	    var relationFieldTR = $("#relationFieldTR");
	 	  var	associatedFieldTR = $("#associatedFieldTR");
	 	  
	 	  var	sysFunctionTR = $("#sysFunctionTR");
	 	 	sysFunctionTR.hide();

	 	 	
			//函数类型
	    functionTypeSlt.change(function() {
	      relationTableSlt = $("select[name=relationTableSlt]");
	      relationFieldSlt = $("select[name=relationFieldSlt]");
	 	  	associatedFieldSlt = $("select[name=associatedFieldSlt]");
	      
	    	var currentSltValue = $(this).val();
	    	if (currentSltValue == "0") {//数据函数
	    		relationTableTR.show();
	    		relationFieldTR.show();
	    		associatedFieldTR.show();

	      	sysFunctionTR.hide();
	      } else if (currentSltValue == "1"){//系统函数
	    	  relationTableTR.hide();
	    	  relationFieldTR.hide();
	    	  associatedFieldTR.hide();

	    	  sysFunctionTR.show();
	      } else {//所有类型
	    	  relationTableTR.hide();
	    	  relationFieldTR.hide();
	    	  associatedFieldTR.hide();
		      sysFunctionTR.hide();
		    }
	    });

			//对应字段
	    registerRelationFieldSltOrReloadAndReRegister();

			//对应表
	    relationTableSlt.change(function() {
	    	var currentSltValue = $(this).val();
	    	
	    	if (currentSltValue == "bdepartment" || currentSltValue == "dclient") {
		    	associatedFieldSlt = $("select[name=associatedFieldSlt]");
	      	$.get("<c:url value='associatedFieldEmptySelect.html' />", 
	      	  function(data){
	      			associatedFieldSlt.replaceWith($(data).html());
	        });
	      }
	        
	    	if (currentSltValue == "dorder") {
	    		relationFieldTR.show();
	    		associatedFieldTR.show();
	      	$.get("<c:url value='relationFieldOrderSelect.html' />", 
	      	  function(data){
	        		relationFieldSlt.replaceWith($(data).html());
	          			
	        		registerRelationFieldSltOrReloadAndReRegister(); 		           		
	        });
	      } else if (currentSltValue == "bdepartment") {
	    		relationFieldTR.show();
	    		associatedFieldTR.show();
	        	$.get("<c:url value='relationFieldDepartmentSelect.html' />", 
	 	      	  function(data){
	        			relationFieldSlt.replaceWith($(data).html());
	          			
	        			registerRelationFieldSltOrReloadAndReRegister(); 		      			
	 	        });
	      } else if (currentSltValue == "dclient") {
	    		relationFieldTR.show();
	    		associatedFieldTR.show();
	      	$.get("<c:url value='relationFieldClientSelect.html' />", 
	      	  function(data){
	      			relationFieldSlt.replaceWith($(data).html());
	      			
	      			registerRelationFieldSltOrReloadAndReRegister(); 			
	        });
	      } else {
	    		relationFieldTR.hide();
	    		associatedFieldTR.hide();
		    }

	    });
	    
	    function registerRelationFieldSltOrReloadAndReRegister() {
	  	  	relationFieldSlt = $("select[name=relationFieldSlt]");//Reload Slt
	       	relationFieldSlt.change(function() {
		       	var currentSltValue = $(this).val();
		       	if (currentSltValue == "clientid") {//客户编号 -> 客户表
		       	  associatedFieldSlt = $("select[name=associatedFieldSlt]");

		   	     	$.get("<c:url value='associatedFieldClientSelect.html' />", 
		   	    	  function(data){
		   	     			associatedFieldSlt.replaceWith($(data).html());
		   	      	});
		         } else if (currentSltValue == "loaddeptid" || currentSltValue == "unloadeptid") {//起运地公司编号 or 目的地公司编号 -> 组织结构表
			       	  associatedFieldSlt = $("select[name=associatedFieldSlt]");

								var url = "<c:url value='associatedFieldLoadDepartmentSelect.html' />";
								if (currentSltValue == "unloadeptid") {
									url = "<c:url value='associatedFieldUnloadDepartmentSelect.html' />";
								}
		   	     		$.get(url, 
		    	    	  function(data){
		    	     			associatedFieldSlt.replaceWith($(data).html());
		    	      	});
	         } else {
		       	  associatedFieldSlt = $("select[name=associatedFieldSlt]");

		 	     		$.get("<c:url value='associatedFieldEmptySelect.html' />", 
		  	    	  function(data){
		  	     			associatedFieldSlt.replaceWith($(data).html());
		  	      	});
	         }
	       });
	    }
	});
</script>